/*
 * 文 件    名:  MyCartService.java
 * 创建日期:  2012-10-10下午4:16:51
 * 版          权:  Epro Technologies Co., Ltd. Copyright 2011-2012,  All rights reserved
 * 作          者:  dc051
 */
package com.weipu.common.facade.content.api;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import com.weipu.common.facade.content.BaseService;
import com.weipu.common.facade.content.ContentURI;
import com.weipu.common.facade.content.colum.MyCartColumn;
import com.weipu.common.facade.content.colum.OnekeyFilterColumn;
import com.weipu.common.facade.content.impl.MyCartProviderImpl;
import com.weipu.common.facade.content.impl.OnekeyFilterProviderImpl;
import com.weipu.common.facade.model.CatelogModel;
import com.weipu.common.facade.model.OrderProductModel;
import com.weipu.common.util.JsonFormat;
import com.weipu.common.util.Logger;

/** 
 * @Description 管理 service
 * @version [版本号, 2012-10-10]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author dc051
 */
public class OnekeyFilterService extends BaseService
{
    
    OnekeyFilterProviderImpl onekeyFilterImpl = new OnekeyFilterProviderImpl();
    
    /**
     * 封装cursor 为java bean
     * @param cursor
     * @return [参数说明]
     * @return ArrayList<TemplateModel> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    private ArrayList<CatelogModel> getListByCursor(Cursor cursor)
    {
        if (isNullOrEmpty(cursor))
        {
            close(cursor);
            return null;
        }
        ArrayList<CatelogModel> modelList = new ArrayList<CatelogModel>(cursor.getCount());
        int[] idx =
            {	cursor.getColumnIndex(OnekeyFilterColumn._ID), 
        		cursor.getColumnIndex(OnekeyFilterColumn.CUSTOMERID),
                cursor.getColumnIndex(OnekeyFilterColumn.CATEGORYID), 
                cursor.getColumnIndex(OnekeyFilterColumn.CATEGORYNAME),
                cursor.getColumnIndex(OnekeyFilterColumn.ISSHOW)
            };
        
        do
        {
        	CatelogModel model = new CatelogModel();
            if (-1 != idx[0])
            {
                model.setId(cursor.getInt(idx[0]));
            }
            
            //CICID
            if (-1 != idx[1])
            {
                model.setCustomerID(cursor.getLong(idx[1]));
            }
            
            //CategoryID
            if (-1 != idx[2])
            {
                model.setBrowseID(cursor.getInt(idx[2]));
            }
            //CategoruName
            if (-1 != idx[3])
            {
                model.setShortName(cursor.getString(idx[3]));
            }
            //isShow
            if (-1 != idx[4])
            {
                model.setShow("true".equals(cursor.getString(idx[4]))?true:false);
            }
            modelList.add(model);
        } while (cursor.moveToNext());
        close(cursor);
        return modelList;
    }
    
    /**
     * 获取指定类型的模板列表
     * @param type 日报/周报
     * {@link ContentConstant.Common#CONTENT_TYPE_DAILY}
     * {@link ContentConstant.Common#CONTENT_TYPE_WEEKLY}
     * @return [参数说明]
     * @return ArrayList<TemplateModel> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public ArrayList<CatelogModel> getListByCustomerID(Context context, long customerID)
    {
        if (!check(context, customerID))
        {
            return null;
        }
        return getListByCursor(context.getContentResolver().query(ContentURI.OnekeyFilterList.CONTENT_URI_ALL,
            null,
            OnekeyFilterColumn.CUSTOMERID+"=?",
            new String[]{customerID+""},
            null));
    }
    
    public ArrayList<CatelogModel> getBySelection(Context context, String selection, String... args)
    {
        if (!check(context))
        {
            return null;
        }
        return getListByCursor(context.getContentResolver().query(ContentURI.OnekeyFilterList.CONTENT_URI_ALL,
            null,
            selection,
            args,
            null));
        
    }
    
    /**
     * 根据id查询指定模板
     * @param context
     * @param id 模板id
     * @return [参数说明]
     * @return ArrayList<TemplateModel> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public CatelogModel getById(Context context, long id)
    {
        if (!check(context))
        {
            return null;
        }
        if (id < 0)
        {
            return null;
        }
        ArrayList<CatelogModel> modelList =
            getListByCursor(context.getContentResolver()
                .query(ContentUris.withAppendedId(ContentURI.OnekeyFilterList.CONTENT_URI_BY_ID, id),
                    null,
                    null,
                    null,
                    null));
        if (null == modelList || modelList.isEmpty())
        {
            return null;
        }
        return modelList.get(0);
    }
    
    
    /**
     * 自定义条件查询并返回游标
     * @param context
     * @param projection
     * @param selection
     * @param args
     * @return [参数说明]
     * @return Cursor [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public Cursor queryBySelection(Context context, String[] projection, String selection, String[] args,
        String sortOrder)
    {
        return context.getContentResolver().query(ContentURI.ContentMyCart.CONTENT_URI_ALL,
            projection,
            selection,
            args,
            sortOrder);
    }
    
    /**
                  * 保存
     * @param context
     * @param model
     * @return [参数说明]
     * @return long [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public long save(Context context, CatelogModel model)
    {
        if (!check(context, model))
        {
            return -1;
        }
        ContentValues map = new ContentValues();
        map.put(OnekeyFilterColumn.CUSTOMERID, model.getCustomerID());
        map.put(OnekeyFilterColumn.CATEGORYID, model.getBrowseID());
        map.put(OnekeyFilterColumn.CATEGORYNAME, model.getShortName());
        map.put(OnekeyFilterColumn.ISSHOW, model.isShow()?"true":"false");
        try
        {
            Uri uri = onekeyFilterImpl.insert(ContentURI.OnekeyFilterList.CONTENT_URI_ALL, map);
            Logger.d("uri:" + uri);
        }
        catch (Exception e)
        {
            return -1;
        }
        
        Long result = map.getAsLong(MyCartColumn._ID);
        
        long returnId = (null != result ? result : -1);
        
        return returnId;
    }
    
    
    /**
     * 根据id删除指定模板
     * @param context
     * @param id 模板id
     * @return [参数说明]
     * @return ArrayList<TemplateModel> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public int deleteByCustomerID(Context context, long customerID)
    {
        if (!check(context))
        {
            return -1;
        }
        
        int deleteRow = -1;
        
        try
        {
            deleteRow =
                onekeyFilterImpl.delete(ContentURI.OnekeyFilterList.CONTENT_URI_ALL,
                    OnekeyFilterColumn.CUSTOMERID + "=?",
                    new String[] {String.valueOf(customerID)});
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return deleteRow;
    }
    
    /**
     * 自定义条件删除
     * @param context
     * @param selection 删除条件
     * @param args 条件参数
     * @return [参数说明]
     * @return int [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public int deleteTemplateBySelection(Context context, String selection, String[] args)
    {
        if (!check(context))
        {
            return -1;
        }
        
        int deleteRow = -1;
        try
        {
            deleteRow = context.getContentResolver().delete(ContentURI.OnekeyFilterList.CONTENT_URI_ALL, selection, args);
        }
        catch (Exception e)
        {
            //ignore
        }
        return deleteRow;
    }
    
}
